<?php

/**
 * Smithy navigation item.
 *
 */
class Smithy_Navigation_Item
{
    /**
     * The navigation object, the item is in.
     *
     * @var Smithy_Navigation
     */
    protected $_navigation = null;
    protected $_text = 'Smithy Navigation Item';
    protected $_url = '';
    protected $_loginRequirement = Smithy_Navigation::LOGIN_NOT_REQUIRED;
    protected $_className = null;
    protected $_activeClassName = null;
    protected $_isActive = false;
    protected $_actualItemKey = null;
    protected $_subItems = array();
    protected $_auth = null;


    /**
     *
     */
    function __construct ()
    {}

    /**
     * Factory method
     *
     * @return Smithy_Navigation_Item
     */
    public static function factory()
    {
        return new Smithy_Navigation_Item();
    }

    /**
     * Renders the item.
     *
     * @return string
     */
    public function render()
    {
        if (Smithy_Navigation::LOGIN_REQUIRED === $this->_loginRequirement) {
            if (null === $this->getAuth() or !$this->getAuth()->hasIdentity()) {
                return '';
            }
        } elseif(Smithy_Navigation::LOGIN_FORBIDDEN === $this->_loginRequirement) {
            if ($this->getAuth()->hasIdentity()) {
                return '';
            }
        }

        $source = '<li class="' .
            ($this->getIsActive() ? $this->getActiveClassName() : $this->getClassName())
             . '" ><a href = "' . $this->getUrl() . '" >' . $this->getText() . '</a>';

        if (0 < count($this->_subItems)) {
            $source .= '<ul>';
            foreach ($this->_subItems as $key => $subItem) {
                $subItem->setIsActive($this->_actualItemKey === $key);
                $source .= $subItem->render();
            }
            $source .= '</ul>';
        }

        return $source . '</li>';
    }

    /**
     * Adds a new navigation item to the menu.
     *
     * @param string $key
     * @param Smithy_Navigation_Item $item
     * @return Smithy_Navigation_Item
     */
    public function addItem($key, Smithy_Navigation_Item $item)
    {
        $this->_subItems[$key] = $item->setNavigation($this->_navigation);
        return $this;
    }

    /**
     * Returns the navigation item requested by its key.
     *
     * @param string $key
     * @return Smithy_Navigation_Item
     */
    public function getItemByKey($key)
    {
        if (array_key_exists($key, $this->_subItems)) {
            return $this->_subItems[$key];
        }

        return null;
    }

    /**
     * Sets the actual menu item.
     *
     * @return Smithy_Navigation_Item
     */
    public function setActualItemKey($key)
    {
        if (array_key_exists($key, $this->_subItems)) {
            $this->_actualItemKey = $key;
        }
        return $this;
    }

    /**
     * The active item's class attribute.
     *
     * @return string
     */
    public function getActiveClassName ()
    {
        return empty($this->_activeClassName) ? $this->_navigation->getDefaultActiveItemClass() : $this->_activeClassName;
    }

    /**
     * The item's class attribute.
     *
     * @return string
     */
    public function getClassName ()
    {
        return empty($this->_className) ? $this->_navigation->getDefaultItemClass() : $this->_className;
    }

    /**
     * The item's login requirement.
     *
     * @return string
     */
    public function getLoginRequirement ()
    {
        return $this->_loginRequirement;
    }

    /**
     * The item's contanier navigation object.
     *
     * @return Smithy_Navigation
     */
    public function getNavigation ()
    {
        return $this->_navigation;
    }

    /**
     * The item's text.
     *
     * @return string
     */
    public function getText ()
    {
        return $this->_text;
    }

    /**
     * The item's url.
     *
     * @return string
     */
    public function getUrl ()
    {
        return $this->_url;
    }

    /**
     * The item's active state.
     *
     * @return bool
     */
    public function getIsActive()
    {
        return $this->_isActive;
    }

    /**
     * Returns the auth object.
     *
     * @return Zend_Auth
     */
    public function getAuth()
    {
        return (null === $this->_auth ? Zend_Auth::getInstance() : $this->_auth);
    }

    /**
     * Sets the active item's class attribute.
     *
     * @param string $_activeClassName
     * @return Smithy_Navigation_Item
     */
    public function setActiveClassName ($_activeClassName)
    {
        $this->_activeClassName = $_activeClassName;
        return $this;
    }

    /**
     * Sets the item's class attribute.
     *
     * @param string $_className
     * @return Smithy_Navigation_Item
     */
    public function setClassName ($_className)
    {
        $this->_className = $_className;
        return $this;
    }

    /**
     * Sets the item's login requirement.
     *
     * @param string $_loginRequirement
     * @return Smithy_Navigation_Item
     */
    public function setLoginRequirement ($_loginRequirement)
    {
        $this->_loginRequirement = $_loginRequirement;
        return $this;
    }

    /**
     * Sets the container navigation object.
     *
     * @param Smithy_Navigation $navigation
     * @return Smithy_Navigation_Item
     */
    public function setNavigation(Smithy_Navigation $navigation)
    {
        $this->_navigation = $navigation;
        return $this;
    }

    /**
     * Sets the item's text to display.
     *
     * @param string $_text
     * @return Smithy_Navigation_Item
     */
    public function setText ($_text)
    {
        $this->_text = $_text;
        return $this;
    }

    /**
     * Sets the items url.
     *
     * @param string $_url
     * @return Smithy_Navigation_Item
     */
    public function setUrl ($_url)
    {
        $this->_url = $_url;
        return $this;
    }

    /**
     * Sets the item's active state.
     *
     * @param bool $isActive
     * @return Smithy_Navigation_Item
     */
    public function setIsActive($isActive)
    {
        $this->_isActive = (bool)$isActive;
        return $this;
    }

    /**
     * Sets the auth object.
     *
     * @param Zend_Auth $auth
     * @return Smithy_Navigation_Item
     */
    public function setAuth($auth)
    {
        $this->_auth = $auth;
        return $this;
    }

}
